﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;

namespace Zor_DAL
{
   public class Zor_Lease_DAL<T> where T : class
    {
        /// <summary>
        /// 租赁管理-修改
        /// </summary>
        /// <param name="lease">楼宇对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
       public static BaseAnswer UpdateLease(Zor_Lease lease, DateTime startTime, DateTime endTime)
        {
            var sql = "";
            var st = TimeHelper.ToTimeStamp(startTime);   //租赁开始时间转时间戳格式
            var et = TimeHelper.ToTimeStamp(endTime);     //租赁结束时间转时间戳格式
            sql += "if not exists (select Lease_Id,Acceptance_State from dbo.Zor_Acceptance where Acceptance_State>1 and Lease_Id =@Lease_Id)update [Zor_Lease] set Tenant_Id=@Tenant_Id, HouseShopId=@HouseShopId,Lease_StartTime=@Lease_StartTime,Lease_EndTime=@Lease_EndTime,Lease_Rent=@Lease_Rent,Lease_Remarks=@Lease_Remarks  where Lease_Id = @Lease_Id";
            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, new Zor_Lease { Tenant_Id = lease.Tenant_Id, HouseShopId = lease.HouseShopId, Lease_StartTime = st.ToString(), Lease_EndTime = et.ToString(), Lease_Rent = lease.Lease_Rent, Lease_Remarks = lease.Lease_Remarks, Lease_Id = lease.Lease_Id });
            return bar;
        }
       /// <summary>
       /// 租赁管理-新增
       /// </summary>
       /// <param name="lease">租赁对象</param>
       /// <returns>执行结果：1 成功 0失败</returns>
       public static BaseAnswer InsertLease(Zor_Lease lease, DateTime startTime, DateTime endTime)
       {
           var st = TimeHelper.ToTimeStamp(startTime);      //租赁开始时间转时间戳格式
           var et = TimeHelper.ToTimeStamp(endTime);       //租赁结束时间转时间戳格式           
           var sql = @"if not exists(select HouseShopId  from [V_Lease] where HouseShopId =@HouseShopId and statue = '正常租赁' ) 
                        insert into Zor_Lease values 
                        (@Tenant_Id,@HouseShopId,@Lease_StartTime,@Lease_EndTime,@Lease_Rent,@User_Id,@Lease_CreateTime,@Lease_Remarks,@Lease_Type)";

           BaseAnswer bar = DapperInsert<T>.DoInsert(sql, new
           {
               Tenant_Id = lease.Tenant_Id, 
               HouseShopId = lease.HouseShopId,
               Lease_Type = lease.Lease_Type,
               Lease_StartTime = st,
               Lease_EndTime = et,
               Lease_Rent = lease.Lease_Rent , 
               Lease_Remarks = lease.Lease_Remarks, 
               User_Id = RequestSession.GetSessionUser().User_Id,
               Lease_CreateTime = TimeHelper.ToTimeStamp(DateTime.Now)
           });
           return bar;
       }

       /// <summary>
       /// 删除租赁信息
       /// </summary>
       /// <param name="id">Id集合</param>
       /// <returns></returns>
       public static BaseAnswer DeleteLease(int[] id)
       {
           var sql = @"if not exists(select Lease_Id from [Zor_Acceptance] where Lease_Id in @Lease_Id ) 
                        delete from [Zor_Lease]  where  Lease_Id in @Lease_Id";
           BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { Lease_Id = id.ToArray<int>() });
           return bar;
       }

       /// <summary>
       /// 获取房号下拉列表
       /// </summary>
       /// <returns></returns>
       public static List<T> GetHsNumber()
       {
           var sql = "select Hs_Id,Hs_Number from dbo.Zor_Hous";
           List<T> listHsNumber = DapperQuery<T>.DoQueryList(sql, null);
           return listHsNumber;
       }

       /// <summary>
       /// 获取租户下拉列表
       /// </summary>
       /// <returns></returns>
       public static List<T> GetTenant()
       {
           var us = RequestSession.GetSessionUser().User_Id;
           var sql = "select Tenant_Id,Tenant_Name from dbo.Zor_Tenant where Tenant_CreateName = " + us + "";
           List<T> listTenant = DapperQuery<T>.DoQueryList(sql, null);
           return listTenant;
       }

       /// <summary>
       /// 获取租赁管理列表（不分页）
       /// create 刘云飞 by 2017/04/06   
       /// </summary>
       /// <returns></returns>
       public static List<T> SelectViewLease(string where = null)
       {
           string strwhere = "";
           if (!string.IsNullOrEmpty(where))
           {
               strwhere = "1=1 and " + where;
           }
           var sql = "select * from [V_Lease] where " + strwhere + "order by Lease_Id ";
           return DapperQuery<T>.DoQueryList(sql, null);
       }
      

       /// <summary>
       /// 获取租赁楼宇信息
       /// </summary>
       /// <returns></returns>
       public static List<T> GetBuildname(int Hs_Falg)
       {

           var sql = "select Dt_Id,Bd_Id,Bd_Name from Zor_Building  where Dt_Id = " + RequestSession.GetSessionUser().DT_Id + " and Bd_Flage=" + Hs_Falg + " group by Dt_Id,Bd_Id,Bd_Name order by Bd_Name";
           return DapperQuery<T>.DoQueryList(sql, null);
       }
       /// <summary>
       /// 获取租赁单元信息
       /// </summary>
       /// <returns></returns>
       public static List<T> GetUimName(int Bd_Id)
       {
           var sql = "select Bd_Id,Uim_Id ,Uim_Name  from Zor_UnitInfor where BD_Id = " + Bd_Id + "  group by Bd_Id,Uim_Id,Uim_Name";
           return DapperQuery<T>.DoQueryList(sql, null);
       }

       /// <summary>
       /// 获取当前房屋、商铺信息
       /// </summary>
       /// <returns></returns>
       public static List<T> GetHousName(int Uim_Id)
       {
           var sql = " select Hs_id, Hs_Number ,Uf_Id, Hs_HousUse  from Zor_Hous where Uf_Id = " + Uim_Id + "  ";
           return DapperQuery<T>.DoQueryList(sql, null);
       }

      /// <summary>
      /// 获取房屋用途
      /// </summary>
      /// <param name="Hs_Id"></param>
      /// <returns></returns>
       public static List<T> GetHousUse(int Hs_Id)
       {
           var sql = " select  Hs_id,Hs_HousUse  from Zor_Hous where Hs_Id = " + Hs_Id + "  ";
           return DapperQuery<T>.DoQueryList(sql, null);
       }
    }
}
